let dev = /^dev.+$/.test(window.document.domain) ? 'dev.' : '';
let offline = /^offline.+$/.test(window.document.domain) ? 'offline.' : '';
let prefix = offline != '' ? offline : dev;
let platformId = 333

let reqUrl = {
    userInfo: `http://128.196.150.85:1350/v1/api/safe/system/auth/user/info`,
    auth: `http://128.196.150.85:1350/v1/api/safe/system/auth/user/auths?id=${platformId}`,
    state: `http://128.196.150.85:1350/v1/api/unsafe/system/auth/bind/weixin/state`,
    status: `http://128.196.150.85:1350/v1/api/unsafe/system/auth/bind/weixin/status?state=`,
}

const request = function(url, token) {
    let xhr = new XMLHttpRequest()
    return new Promise((resolve, reject) => {
        xhr.open('POST', url);
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    let resData = JSON.parse(xhr.responseText)
                    if (resData.code == '200') {
                        resolve(resData.object)
                    } else if (resData.code == 401) {
                        reject({ code: '401', message: '登录超时' })
                    } else {
                        let message = resData.message || '服务异常'
                        reject({ code: '500', message: message })
                    }
                } else {
                    reject({ code: '500', message: '请求超时' })
                }
            }
        };
        xhr.setRequestHeader('Token', token)
        xhr.send()
    })
}


const requestBind = function(url, token) {
    let xhr = new XMLHttpRequest()
    return new Promise((resolve, reject) => {
        xhr.open('POST', url);
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    let resData = JSON.parse(xhr.responseText)
                    if (resData.code == '200') {
                        resolve(resData.object)
                    } else if (resData.code == 201) {
                        resolve({ code: '201', message: '' })
                    } else if (resData.code == 204) {
                        resolve({ code: '204', message: '二维码已失效' })
                    } else {
                        let message = resData.message || '服务异常'
                        resolve({ code: '500', message: message })
                    }
                } else {
                    reject({ code: '500', message: '请求超时' })
                }
            }
        };
        xhr.setRequestHeader('Token', token)
        xhr.send()
    })
}


export function getuserInfo(token) {
    return new Promise((resolve, reject) => {
        request(`${reqUrl.userInfo}`, token).then((data) => {
            resolve(data)
        }).catch((err) => {
            reject(err)
        })
    })
}

export function getAuth(token) {
    return new Promise((resolve, reject) => {
        request(`${reqUrl.auth}`, token).then((data) => {
            resolve(data)
        }).catch((err) => {
            reject(err)
        })
    })
}

export function getState(token) {
    return new Promise((resolve, reject) => {
        request(`${reqUrl.state}`, token).then((data) => {
            resolve(data)
        }).catch((err) => {
            reject(err)
        })
    })
}

export function getStatus(token, state) {
    return new Promise((resolve, reject) => {
        requestBind(`${reqUrl.status}` + state, token).then((data) => {
            resolve(data)
        }).catch((err) => {
            reject(err)
        })
    })
}